package com.mycountsnotes.common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static DatabaseHelper instance;
	private String dbName = DBConfig.DB_NAME;
	private int dbVersion = DBConfig.DB_VERSION;
	
    /**
     * 单一实例
     */
    public static DatabaseHelper getInstance(Context context){
        if(instance==null){
            instance=new DatabaseHelper(context);
        }
        return instance;
    }
    
    /**
     * 单一实例
     */
    public static DatabaseHelper getInstance(Context context,String name){
        if(instance==null){
            instance=new DatabaseHelper(context,name);
        }
        return instance;
    }
	
	/**
	 * context上下文；name为数据库的名字；factory游标对象；version版本号
	 * */
	public DatabaseHelper(Context context, String name, CursorFactory factory,int version) {
		super(context, name, factory, version);
		/*初始化数据库
		 * [getReadableDatabase() 创建或打开一个只读数据库]
		 * [getWritableDatabase() 创建或打开一个读写数据库]
		 * */
		this.dbName = name;
		this.dbVersion = version;
	}
	
	/**
	 * context上下文；factory游标对象；version版本号
	 * */
	public DatabaseHelper(Context context, CursorFactory factory,int version) {
		super(context, DBConfig.DB_NAME, factory, version);
		this.dbName = DBConfig.DB_NAME;
		this.dbVersion = version;
	}

	/**
	 * context上下文；factory游标对象；
	 * */
	public DatabaseHelper(Context context, CursorFactory factory) {
		super(context, DBConfig.DB_NAME, factory, DBConfig.DB_VERSION);
		this.dbName = DBConfig.DB_NAME;
		this.dbVersion = DBConfig.DB_VERSION;
	}
	
	/**
	 * context上下文；factory游标对象；
	 * */
	public DatabaseHelper(Context context, String name) {
		super(context, DBConfig.DB_NAME, null, DBConfig.DB_VERSION);
		this.dbName = name;
		this.dbVersion = DBConfig.DB_VERSION;
	}
	
	/**
	 * context上下文；factory游标对象；
	 * */
	public DatabaseHelper(Context context) {
		super(context, DBConfig.DB_NAME, null, DBConfig.DB_VERSION);
		this.dbName = DBConfig.DB_NAME;
		this.dbVersion = DBConfig.DB_VERSION;
	}
	
	/**
	 * 创建数据时调用
	 * */
	@Override
	public void onCreate(SQLiteDatabase db) {
		 db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");   
	}

	/**
	 * 更新数据时调用
	 * */
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the dbName
	 */
	public String getDbName() {
		return dbName;
	}
	
	/**
	 * @param dbName the dbName to set
	 */
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	
	/**
	 * @return the dbVersion
	 */
	public int getDbVersion() {
		return dbVersion;
	}
	
	/**
	 * @param dbVersion the dbVersion to set
	 */
	public void setDbVersion(int dbVersion) {
		this.dbVersion = dbVersion;
	}
}